package com.example.controller;

import com.example.entity.*;
import com.example.service.impl.StuInforServiceImpl;
import com.example.util.DateUtil;
import com.example.util.RandomNum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
public class StuInforController {
    @Autowired
    StuInforServiceImpl stuInforService;


    @PostMapping
    @ResponseBody
    @RequestMapping("/student/add/{green}")
    public ResponseEntity addStudent(@RequestBody StudentInformation studentInformation,@PathVariable String green){
        try{
            if(studentInformation==null){
                return ResponseEntity.err("学生对象为空");
            }
            studentInformation.setGreenCode(green);
            String ID=stuInforService.adduStuInfor(studentInformation);
            if (!studentInformation.getStudentId().equals(ID)){
                return ResponseEntity.err(ID);
            }
            log.info("addStuInfor成功:"+ID);
            return ResponseEntity.ok(studentInformation);
        }catch (Exception e){
            e.printStackTrace();
            log.error("addStuInfor失败:"+e.getMessage());
            return ResponseEntity.err(e.getMessage());
        }
    }

    /**
     * 查询结果信息
     * @param id
     * @return
     */
    @GetMapping
    @ResponseBody
    @RequestMapping("/student/result/{id}")
    public ResponseEntity queryResult(@PathVariable String id){
        List<ResultVue> list = new ArrayList<>();
        Result result = stuInforService.queryResult(id);
        if (result.getFirstTrialResult().equals("1")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("初审通过");
            resultVue.setType("success");
            resultVue.setColor("#0bbd87");
            list.add(resultVue);
        }else if(result.getFirstTrialResult().equals("0")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("初审未通过");
            resultVue.setType(" warning");
            resultVue.setColor("#cc3300");
            list.add(resultVue);
        }else if (result.getFirstTrialResult().equals("2")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("未评审");
            list.add(resultVue);
        }
        if(result.getVoteResult().equals("1")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("投票通过");
            resultVue.setType("success");
            resultVue.setColor("#0bbd87");
            list.add(resultVue);
        } else  if (result.getVoteResult().equals("0")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("投票未通过");
            resultVue.setType(" warning");
            resultVue.setColor("#cc3300");
            list.add(resultVue);
        }else if (result.getVoteResult().equals("2")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("未评审");
            list.add(resultVue);
        }
        if(result.getIntermediateResult().equals("1")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("学院通过");
            resultVue.setType("success");
            resultVue.setColor("#0bbd87");
            list.add(resultVue);
        } else  if (result.getIntermediateResult().equals("0")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("学院未通过");
            resultVue.setType(" warning");
            resultVue.setColor("#cc3300");
            list.add(resultVue);
        }else if (result.getIntermediateResult().equals("2")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("未评审");
            list.add(resultVue);
        }
        if(result.getFinalResult().equals("1")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("评审成功");
            resultVue.setType("success");
            resultVue.setColor("#0bbd87");
            list.add(resultVue);
        } else  if (result.getFinalResult().equals("0")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("评审失败");
            resultVue.setType(" warning");
            resultVue.setColor("#cc3300");
            list.add(resultVue);
        }else if (result.getFinalResult().equals("2")){
            ResultVue resultVue = new ResultVue();
            resultVue.setContent("未评审");
            list.add(resultVue);
        }
        return ResponseEntity.ok(list);
    }

    @PostMapping
    @ResponseBody
    @RequestMapping("/student/addFile/{stu_id}")
    public ResponseEntity UploadFiles(@RequestParam("enclosureFile") MultipartFile file, HttpServletRequest request,
                                      @PathVariable String stu_id)throws Exception{
        StudentFile studentFile=new StudentFile();

        //获得文件原始名称
        String oldFileName=file.getOriginalFilename();
        //获得文件后缀
        String extension="."+ FilenameUtils.getExtension(file.getOriginalFilename());
        //生成新的文件名称
        String newFileName=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ UUID.randomUUID().toString().replace("-","")+extension;
        //文件大小
        long size=file.getSize();
        //文件类型
        String type=file.getContentType();
        //目录
        String realPath= ResourceUtils.getURL("classpath").getPath()+"/static/student_file";
        String dateDirPath=realPath+"/"+new SimpleDateFormat("yyyy-MM-dd").format(new Date());


        //文件ID
        Integer file_id= RandomNum.getRandomID(6);
        studentFile.setDateDirPath(dateDirPath);
        studentFile.setStu_id(stu_id);
        studentFile.setFile_id(String.valueOf(file_id));
        studentFile.setFile_name(newFileName);
        studentFile.setSize(String.valueOf(size));
        studentFile.setFile_oldname(oldFileName);
        studentFile.setType(type);
        //获取当前时间
        String nowtime= DateUtil.getCurrentDate();
        studentFile.setTime(nowtime);
         String s=stuInforService.saveFile(studentFile);
         if (!s.equals(studentFile.getStu_id())){
             return ResponseEntity.err(s);
         }
        File dateDir=new File(dateDirPath);
        if(!dateDir.exists())dateDir.mkdirs();
        //文件
        file.transferTo(new File(dateDir,newFileName));
        return ResponseEntity.ok(s);
    }

}
